package cn.ava.org.service.impl;

import cn.ava.basic.utils.MD5Utils;
import cn.ava.basic.utils.PageList;
import cn.ava.basic.utils.StrUtils;
import cn.ava.org.domain.Department;
import cn.ava.org.domain.Employee;
import cn.ava.org.mapper.EmployeeMapper;
import cn.ava.org.query.EmployeeQuery;
import cn.ava.org.service.IEmployeeService;
import cn.ava.system.domain.Role;
import cn.ava.user.domain.Logininfo;
import cn.ava.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName: EmployeeServiceImpl
 * @Description: 员工服务层实现类
 * @Author: Avavaava
 * @Date: 2022/7/1 9:24
 * @version: 23.53
 *
 */
@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public List<Employee> loadAll() {
        return employeeMapper.loadAll();
    }

    @Override
    public Employee getById(Long id) {
        return employeeMapper.getById(id);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        // 根据id查询出来employee
        Employee employee = employeeMapper.getById(id);
        Long eId = employee.getLogininfoId();
        // 同步删除logininfo表中的数据
        logininfoMapper.delete(eId);
        //
        employeeMapper.deleteEmployeeRole(eId);
        employeeMapper.delete(id);


    }

    @Override
    @Transactional
    public void add(Employee employee) {
        // 初始化方法employee
        initEmployee(employee);
        // 拷贝方法employee到logininfo
        Logininfo logininfo = employee2Logininfo(employee);
        // 添加logininfo进入数据库
        logininfoMapper.add(logininfo);
        // 返回主键,保存logininfo_id进employee字段中
        employee.setLogininfoId(logininfo.getId());
        // 最后执行添加方法
        employeeMapper.add(employee);
        // 初始化角色表
        initRelationTable(employee);
    }

    /**
     * @Title: initRelationTable
     * @Description: 初始化角色表
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/13 20:36
     * @param:
     * @return:
     */
    public void initRelationTable(Employee employee) {
        if (employee.getRoles() != null){
            employeeMapper.deleteEmployeeRole(employee.getId());
            employeeMapper.addEmployeeRole(employee.getId(),employee.getRoles());
        }
    }


    private void initEmployee(Employee employee) {
        // 设置MD5加密
        String password = employee.getPassword();
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = MD5Utils.encrypByMd5(salt + password);
        // 设置盐值
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);
    }


    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        // 拷贝字段
        BeanUtils.copyProperties(employee,logininfo);
        // 设置类型
        logininfo.setType(0);
        return logininfo;
    }



    @Override
    @Transactional
    public void update(Employee employee) {
        Logininfo logininfo = employee2Logininfo(employee);
        // 同步修改logininfo表数据
        employeeMapper.update(employee);
        logininfoMapper.update(logininfo);
        initRelationTable(employee);
    }

    
    /**
     * @Title: queryData
     * @Description: 封装数据返回前端
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/1 9:25
     * @param:  
     * @return: 
     */
    @Override
    public PageList<Employee> queryData(EmployeeQuery employeeQuery) {
        Integer total = employeeMapper.queryCount(employeeQuery);
        List<Employee> rows = employeeMapper.queryData(employeeQuery);
        return new PageList<>(total,rows);
    }

    @Override
    @Transactional
    public void batchDelete(Long[] id) {
        employeeMapper.batchDelete(id);
    }

    @Override
    public Employee getByUsername(String username) {
        return employeeMapper.getByUsername(username);
    }

    @Override
    public Employee getByLogininfoId(Long id) {
        return employeeMapper.getByLogininfoId(id);
    }

    @Override
    public List<Employee> getByShopId(Long id) {
        return employeeMapper.getByShopId(id);
    }

}
